<?php

declare (strict_types=1);

namespace App\Model;

use App\Service\AftersaleService;
use App\Service\FlyOrderService;
use App\Service\ShopDeliveryConfigService;
use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Db;

/**
 * @property int $order_id
 * @property string $order_sn
 * @property int $order_status
 * @property string $order_price
 * @property string $pay_price
 * @property string $product_price
 * @property int $is_pay
 * @property string $paid_price
 * @property int $paid_time
 * @property int $user_id
 * @property string $link_name
 * @property string $address
 * @property string $phone
 * @property int $address_id
 * @property int $shop_id
 * @property string $shop_name
 * @property string $coupon_price
 * @property int $express_tpl_id
 * @property string $express_price
 * @property string $pay_order_sn
 * @property int $pay_order_id
 * @property string $pay_outer_sn
 * @property int $create_time
 * @property int $is_received
 * @property int $received_time
 * @property int $delivery_id
 * @property string $remark
 * @property int $product_num
 * @property string $cancel_reason
 * @property string $cancel_remark
 * @property string $refund_fee
 * @property int $delete_time
 * @property string $delivery_price
 * @property int $order_type
 * @property int $delivery_type
 * @property int $date_time
 * @property string $charge_off_code
 * @property int $charge_type
 * @property string $delivery_distance
 * @property int $delivery_config_id
 * @property int $curr_af_id
 * @property string $pay_type
 * @property string $pay_method
 * @property int $close_time
 * @property-read FlyAddress $with_address_info
 * @property-read Collection|LinkIndustry[] $with_af_can_applys
 * @property-read Collection|FlyOrderProduct[] $with_af_product
 * @property-read Collection|FlyOrderProduct[] $with_can_apply_af_products
 * @property-read Aftersale $with_current_af
 * @property-read ShopDeliveryConfig $with_delivery_config
 * @property-read Collection|FlyOrderProduct[] $with_products
 * @property-read FlyShop $with_shop
 */
class FlyOrder extends Model
{
    /**
     * @var bool
     */
    public $timestamps = false;
    /**
     * @var string
     */
    protected $primaryKey = 'order_id';
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_order';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['order_id' => 'integer', 'order_status' => 'integer', 'is_pay' => 'integer', 'paid_time' => 'integer', 'user_id' => 'string', 'address_id' => 'integer', 'shop_id' => 'integer', 'express_tpl_id' => 'integer', 'pay_order_id' => 'integer', 'create_time' => 'integer', 'is_received' => 'integer', 'received_time' => 'integer', 'delivery_id' => 'integer', 'product_num' => 'integer', 'delete_time' => 'integer', 'order_type' => 'integer', 'delivery_type' => 'integer', 'date_time' => 'integer', 'charge_type' => 'integer', 'delivery_config_id' => 'integer', 'curr_af_id' => 'integer', 'close_time' => 'integer'];

    /**
     * 关联订单商品
     * @return HasMany
     */
    public function with_products()
    {
        return $this->hasMany(FlyOrderProduct::class, 'order_id', 'order_id')
            ->leftJoin('fly_sku', 'fly_order_product.sku_id', '=', 'fly_sku.sku_id')
            ->select(['fly_order_product.*', 'fly_sku.sku_id as sku_can_use']);
    }

    /**
     * 关联订单可申请售后商品
     * @return HasMany
     */
    public function with_can_apply_af_products()
    {
        return $this->hasMany(FlyOrderProduct::class, 'order_id', 'order_id')
            ->leftJoin('fly_order', 'fly_order_product.order_id', '=', 'fly_order.order_id')
            ->whereRaw("wf_fly_order_product.can_af_apply_duration*86400+paid_time >= " . time() . " and wf_fly_order_product.free_num>0 and wf_fly_order_product.support_af_types!=''") // 有效期内可申请
            ->orWhereRaw("(wf_fly_order_product.can_af_apply_duration=-1 and wf_fly_order_product.free_num>0 and wf_fly_order_product.support_af_types!='')")
            ->select(['fly_order_product.*', 'fly_order.paid_time']);
    }

    /**
     * 关联订单可申请美食售后商品
     * @return HasMany
     */
    public function with_can_apply_food_af_products()
    {
        return $this->hasMany(FlyOrderProduct::class, 'order_id', 'order_id')
            ->leftJoin('fly_order', 'fly_order_product.order_id', '=', 'fly_order.order_id')
            ->where('fly_order.order_style', '=', 3) // 订座订单可申请商品
            ->where('fly_order_product.free_num', '>', 0)
            ->where('fly_order.paid_price', '<>', 0);

    }

    /**
     * 关联商家配送配置
     * @return HasOne
     */
    public function with_delivery_config()
    {
        return $this->hasOne(ShopDeliveryConfig::class, 'id', 'delivery_config_id');
    }

    /**
     * 集散地
     * @return HasOne
     */
    public function with_shop()
    {
        return $this->hasOne(FlyShop::class, 'id', 'shop_id')
            ->with(['with_join_dc'])
            ->with(['certificate_att'])
            ;
    }

    /**
     * 获取订单可售后的商品
     * @return HasMany|Builder
     */
    public function with_af_product()
    {
        return $this->hasMany(FlyOrderProduct::class, 'order_id', 'order_id')
            ->leftJoin('fly_link_industry', 'fly_order_products.industry_id', '=', 'fly_link_industry.industry_id')
            ->where('fly_link_industry.type', '=', 5)
            ->where('fly_order_product.locked_num', '>', 0);
    }

    /**
     * 获取行业支持售后类型
     * @return HasMany
     */
    public function with_af_can_applys()
    {
        return $this->hasMany(LinkIndustry::class, 'industry_id', 'industry_id')
            ->leftJoin('fly_after_sell_set', 'link_industry.link_id', '=', 'fly_after_sell_set.id')
            ->where('link_industry.type', '=', 4)
            ->select(['link_industry.id', 'link_industry.link_id', 'link_industry.from_id', 'fly_aftersale_set.type']);
    }

    /**
     * 关联收货地址信息
     * @return HasOne
     */
    public function with_address_info()
    {
        return $this->hasOne(FlyAddress::class, 'id', 'address_id');
    }

    /**
     * 获取当前对应处理中售后单
     * @return HasOne
     */
    public function with_current_af()
    {
        return $this->hasOne(Aftersale::class, 'order_id', 'order_id')
            ->orderBy('af_id', 'desc');
//            ->whereIn('af_status', [AftersaleService::STATUS_WAIT_CHECK, AftersaleService::STATUS_CHECKED]);
    }

    /**
     * 获取物流信息
     * @return HasOne
     */
    public function with_delivery_log()
    {
        return $this->hasOne(FlyShopDelivery::class, 'order_id', 'order_id')
            ->leftJoin('fly_poll_log as fpl', 'fpl.id', '=', 'fly_shop_delivery.poll_id')
            ->with('com_info')
            ->with('with_log');
//            ->whereIn('af_status', [AftersaleService::STATUS_WAIT_CHECK, AftersaleService::STATUS_CHECKED]);
    }

    /**
     * 获取发货单信息
     * @return HasOne
     */
    public function with_delivery()
    {
        return $this->hasOne(FlyShopDelivery::class, 'id', 'delivery_id')
            ->with(['with_poll_log']);
    }
}